//
//  HeadViewVC.swift
//  FirstSwiftProduct
//
//  Created by 知合金服-Mini on 2017/12/6.
//  Copyright © 2017年 范泽华. All rights reserved.
//

import UIKit

class HeadViewVC: BasePushViewController {

    public var headImage:UIImage!
    var headImageView:UIImageView!
    public var fromWhere : Int!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if fromWhere == 1 {
             headImageView = UIImageView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_WIDTH))
            headImageView.image = headImage
        }else{
             headImageView = UIImageView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH - 40, height: SCREEN_WIDTH + 40 ))
             let theQRImage = iconQRcode(string: "我是天才呀，盆友", icon: UIImage.init(named: "father")!)
            headImageView.image = theQRImage
        }
        
        self.titleLabel.text = "个人头像"
        view.backgroundColor = .black
       
        headImageView.center = view.center
        
        view.addSubview(headImageView)
        
        let btn = UIButton.init(type: .custom)
        btn.frame = CGRect(x: SCREEN_WIDTH - 60, y: 26, width: 60, height: 30)
        btn.addTarget(self, action: #selector(chooseAction), for: .touchUpInside)
        view.addSubview(btn)
        btn.setImage(UIImage.init(named: "ProfileLockOn"), for: .normal)
        // Do any additional setup after loading the view.
        
       
        
    }

    
    func chooseAction()  {
        //alertView
        
    }
    
    ////////////////////////######################################################
    func iconQRcode(string:String,icon:UIImage) -> UIImage {
        let filter:CIFilter = CIFilter.init(name: "CIQRCodeGenerator")!
        let data = string.data(using: .utf8)
        
        filter.setValue(data, forKey: "inputMessage")
        var ciimage = filter.outputImage
        ciimage = ciimage?.applying(CGAffineTransform.init(scaleX: 10, y: 10))
        var resultImage = UIImage.init(ciImage: ciimage!)
        
        resultImage = imageWith(icon: icon, QRcodeimg: resultImage)
        return resultImage
    }
    
    func imageWith(icon:UIImage,QRcodeimg:UIImage) -> UIImage {
        
        var myImgSize = icon.size
        if headImageView.size.width > 0{
            myImgSize.width = headImageView.size.width
            myImgSize.height = headImageView.size.height
        }else if myImgSize.width > 0{
            myImgSize = icon.size
        }else{
            myImgSize.width = 140
            myImgSize.height = 140
        }
        //开启上下文
        UIGraphicsBeginImageContext(myImgSize)
        //画原图
        QRcodeimg.draw(in: CGRect(x: 0, y: 0, width: myImgSize.width, height: myImgSize.height))
//        let minH = min(myImgSize.width, myImgSize.height) * 0.2
        let minH = myImgSize.width > myImgSize.height ?  myImgSize.height * 0.2 : myImgSize.width * 0.2
        
        icon.draw(in: CGRect(x: (myImgSize.width - minH) * 0.5, y: (myImgSize.height - minH) * 0.5, width: minH, height: minH))
        
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage!
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
